<template>
  <div class="mod-config">
    <el-form
      :inline="true"
      :model="dataForm"
      @keyup.enter.native="getDataList()"
    >
      <el-form-item label="导出状态">
        <el-select v-model="value" clearable>
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="导出时间">
        <el-date-picker
          v-model="dateRange"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd HH:mm:ss"
          :default-time="['00:00:00', '23:59:59']"
          align="right"
          @change="timeChange"
          clearable
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList('query')">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%"
      :default-sort="{ prop: 'createTime ', order: 'descending' }"
    >
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50"
      >
      </el-table-column>
      <el-table-column
        prop="id"
        header-align="center"
        align="center"
        label="序号"
      >
      </el-table-column>
      <el-table-column
        prop="createTime"
        header-align="center"
        align="center"
        label="导出时间"
        :formatter="formatter"
      >
      </el-table-column>
      <el-table-column
        prop="userName"
        header-align="center"
        align="center"
        label="导出人"
      >
      </el-table-column>

      <el-table-column
        prop="status"
        header-align="center"
        align="center"
        label="导出状态"
      >
        <template slot-scope="scope">
          <div>
            <div v-if="scope.row.status == 0">待导出</div>
            <div v-if="scope.row.status == 1">导出成功</div>
            <div v-if="scope.row.status == 2">导出中</div>
            <div v-if="scope.row.status == 3">导出失败</div>
          </div>
        </template>
      </el-table-column>
      <el-table-column
        prop="updateTime"
        header-align="center"
        align="center"
        label="完成时间"
      >
        <template slot-scope="scope">
          <div v-if="scope.row.status === 1 || scope.row.status === 3">
            {{ scope.row.updateTime }}
          </div>
        </template>
      </el-table-column>
      <el-table-column
        prop="operation"
        header-align="center"
        align="center"
        label="操作"
      >
        <template slot-scope="scope">
          <div>
            <el-button
              type="text"
              :disabled="!scope.row.url"
              @click="handelDownLoad(scope.row)"
            >
              下载</el-button
            >
          </div>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageParams.page"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageParams.limit"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    >
    </el-pagination>
  </div>
</template>
<script>
export default {
  data() {
    return {
      options: [
        {
          value: "0",
          label: "待导出",
        },
        {
          value: "1",
          label: "导出成功",
        },
        {
          value: "2",
          label: "导出中",
        },
        {
          value: "3",
          label: "导出失败",
        },
      ],
      value: "",
      gridData: [],
      projectList: [],
      dataForm: {
        projectId: "",
        endDate: new Date(parseInt(+new Date()))
          .toLocaleString()
          .replace(/[/]+/g, "-"),
        startDate: new Date(parseInt(+new Date() - 3600 * 1000 * 24))
          .toLocaleString()
          .replace(/[/]+/g, "-"),
      },
      dataList: [],
      pageParams: {
        page: 1,
        limit: 10,
      },
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      trackVisible: false,
      infoVisible: false,

      dateRange: [
        new Date(parseInt(+new Date() - 3600 * 1000 * 24 * 30))
          .toLocaleString()
          .replace(/[/]+/g, "-"),
        new Date(parseInt(+new Date())).toLocaleString().replace(/[/]+/g, "-"),
      ],
      dateRangeAll: [
        new Date(parseInt(+new Date() - 3600 * 1000 * 24))
          .toLocaleString()
          .replace(/[/]+/g, "-"),
        new Date(parseInt(+new Date())).toLocaleString().replace(/[/]+/g, "-"),
      ],
    };
  },
  activated() {
    this.getDataList();
  },
  created() {},
  mounted() {
    this.getDataList();
  },
  methods: {
    formatter(row, column) {
      return row.createTime;
      // console.log(row);
    },
    handelDownLoad(row) {
      if (row.url) {
        console.log(row.url);
        window.open(row.url);
        // if(row.url){
        //   const blob = new Blob([row.url], { type: "application/vnd.ms-excel" });
        //   let a = document.createElement("a");
        //   a.download = row.createTime + this.$store.state.user.name + ".xlsx";
        //   a.href = URL.createObjectURL(blob);
        //   document.getElementsByTagName("body")[0].appendChild(a);
        //   a.click();
        //   a.parentNode.removeChild(a);
      }
    },
    // 处理数组
    getArr(e) {
      return e.split(",");
    },
    timeChange(e) {
      if (e) {
        this.dataForm.startDate = e[0];
        this.dataForm.endDate = e[1];
      } else {
        this.dataForm.startDate = null;
        this.dataForm.endDate = null;
      }
    },
    // 处理数组
    getArr(e) {
      return e.split(",");
    },
    // 获取数据列表
    getDataList(flag) {
      if (flag) {
        this.pageParams.page = 1;
      }
      // this.dataListLoading = true;
      console.log(this.dateRange);
      let startDate = this.dateRange ? this.dateRange[0] : "";
      let endDate = this.dateRange ? this.dateRange[1] : "";
      console.log(startDate, endDate);
      let params = { ...this.pageParams };
      if (this.value) {
        params.status = this.value;
      }
      if (startDate) {
        params.startDate = startDate;
      }
      if (endDate) {
        params.endDate = endDate;
      }

      this.$http({
        url: this.$http.adornUrl("/generator/bcarledger/export/page"),
        method: "get",
        params: this.$http.adornParams(params),
      }).then(({ data }) => {
        console.log(data);
        if (data && data.code === 0) {
          this.dataList = data.page.list;
          this.totalPage = data.page.totalCount;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
      });
    },
    sizeChangeHandle(val) {
      this.pageParams.limit = val;
      this.pageParams.page = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageParams.page = val;
      this.getDataList();
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    },
  },
  // 每页数
};
</script>
